package com.sansan.qiangji.realize.early;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sansan.qiangji.VO.early.EarlyWarningListResultVO;
import com.sansan.qiangji.VO.early.EarlyWarningVO;
import com.sansan.qiangji.entity.early.EarlyWarning;
import com.sansan.qiangji.entity.early.example.EarlyWarningExample;
import com.sansan.qiangji.form.early.EarlyWarningListForm;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.service.inter.early.EarlyWarningService;
import com.sansan.qiangji.utils.ConvertUtil;
import com.sansan.qiangji.utils.ResultVOUtil;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import com.sansan.qiangji.utils.pageUtils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhaobc
 * @description
 * @date 2020-11-13
 */
@Service
@Slf4j
public class EarlyWarningReal {
    @Autowired
    private EarlyWarningService earlyWarningService;

    public EarlyWarningListResultVO getEarlyWarningList(EarlyWarningListForm listForm){
        EarlyWarningExample example = new EarlyWarningExample();
        int pageNum = listForm.getPageNum();
        int pageSize = listForm.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<EarlyWarning> EarlyWarningList = earlyWarningService.selectListCondition(listForm);
        PageResult<EarlyWarning> productPageResult = PageUtil.getPageResult(new PageInfo<>(EarlyWarningList));
        EarlyWarningListResultVO earlyWarningListResultVO = new EarlyWarningListResultVO();
        BeanUtils.copyProperties(productPageResult,earlyWarningListResultVO);
        List<EarlyWarningVO> EarlyWarningVOS = new ArrayList<>();
        for(EarlyWarning earlyWarning : productPageResult.getContent()){
            EarlyWarningVO earlyWarningVO = new EarlyWarningVO();
            BeanUtils.copyProperties(earlyWarning,earlyWarningVO);
            EarlyWarningVOS.add(earlyWarningVO);
        }
        earlyWarningListResultVO.setEarlyWarningVOS(EarlyWarningVOS);
        return earlyWarningListResultVO;
    }

    /**
     * 获取超标上下限记录生成到预警表
     */
    public void getEarlyStockList(){
        EarlyWarningExample example = new EarlyWarningExample();
        List<Map<String,Object>> earlyStockList= new ArrayList<Map<String,Object>>();
        earlyStockList = earlyWarningService.getEarlyStockList();

        EarlyWarningExample.Criteria criteria = example.createCriteria();
        criteria.andEarlyTypeEqualTo(1);
        if(earlyStockList.size()>0){
            //清空历史记录
            earlyWarningService.deleteByExample(example);
            //插入符合条件的上下限预警记录
            for (Map<String,Object> early : earlyStockList)
            {
                EarlyWarning earlyWarning = new EarlyWarning();
                earlyWarning.setProductCode(ConvertUtil.ConvertToString(early.get("product_code")));
                earlyWarning.setProductName(ConvertUtil.ConvertToString(early.get("product_name")));
                earlyWarning.setUpperLowerMsg(ConvertUtil.ConvertToString(early.get("msg")));
                earlyWarning.setUpperLowerLimit(ConvertUtil.ConvertToInt(early.get("s_count")));//+上下限数(库存)
                earlyWarning.setInitValue(ConvertUtil.ConvertToInt(early.get("init_value")));//初始值
                earlyWarning.setOverStepNum(ConvertUtil.ConvertToInt(early.get("over_step_num")));//上下限超出数
                earlyWarning.setEarlyType(1);
                earlyWarning.setCreateTime(new Date());
                earlyWarningService.insert(earlyWarning);
            }
        }
        log.info("【预警[上下限]生成】EarlyWarningReal->getEarlyStockList执行成功...]");
    }

    /**
     * 获取超标库龄的入库记录生成到预警表
     */
    public void getEarlyOverdueList(){
        EarlyWarningExample example = new EarlyWarningExample();
        List<Map<String,Object>> earlyOverdueList= new ArrayList<Map<String,Object>>();
        earlyOverdueList = earlyWarningService.getEarlyOverdueList();

        EarlyWarningExample.Criteria criteria = example.createCriteria();
        criteria.andEarlyTypeEqualTo(0);
        if(earlyOverdueList.size()>0){
            //清空历史记录
            earlyWarningService.deleteByExample(example);
            //插入符合条件的上下限预警记录
            for (Map<String,Object> early : earlyOverdueList)
            {
                EarlyWarning earlyWarning = new EarlyWarning();
                earlyWarning.setProductCode(ConvertUtil.ConvertToString(early.get("product_code")));
                earlyWarning.setProductName(ConvertUtil.ConvertToString(early.get("product_name")));
                earlyWarning.setOverdue(ConvertUtil.ConvertToInt(early.get("overdue")));
                earlyWarning.setInitValue(ConvertUtil.ConvertToInt(early.get("storage_age")));//初始值
                earlyWarning.setUpperLowerMsg("");
                earlyWarning.setEarlyType(0);
                earlyWarning.setCreateTime(new Date());
                earlyWarningService.insert(earlyWarning);
            }
        }
        log.info("【预警[库龄]生成】EarlyWarningReal->getEarlyOverdueList执行成功...]");
    }
}
